#include "loginwidget.h"
#include <QPainter>
#include <QMessageBox>
#include <QSettings>
#include <QDebug>
#include <QString>
#include <QMenu>

#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonValue>
#include <QByteArray>
#include <QVariantMap>

#include <QObject>

#include "httpcurl.h"
#include "mylable.h"

LoginWidget::LoginWidget(QWidget *parent) : QWidget(parent)
{
    //初始化窗口大小
    //this->setWindowTitle("登录");
    //setWindowFlags(Qt::FramelessWindowHint);去掉外边框
    resize(800, 500);
    //    setMinimumHeight(800);
    //    setMaximumHeight(800);
    //    setMinimumWidth(2400);
   //    setMaximumWidth(2400);

    //隐藏菜单栏
    //去窗口表框
    setWindowFlags(Qt::FramelessWindowHint | windowFlags());



    //右上角有个按钮关闭
    /**
    MyLable *closeLogin = new MyLable(this);
    closeLogin->setText("x");
    closeLogin->setGeometry(760, -30, 100, 100);
    QFont fc;
    fc.setPointSize(20);
    closeLogin->setFont(fc);
    MyLable *minLogin = new MyLable(this);
    minLogin->setText("-");
    minLogin->setFont(fc);
    minLogin->setGeometry(720, -30, 100, 100);
    //点击x关闭窗口
    //connect(closeLogin, &MyLable::myClicked, this, &LoginWidget::closeWin);
     */




    //账号
    QLabel *nameLabel = new QLabel();
    QFont ft;
    ft.setPointSize(20);
    nameLabel->setText(tr("账号"));
    nameLabel->setFont(ft);

    QLineEdit *nameEdit = new QLineEdit();
    nameEdit->setMaxLength(20);
    nameEdit->setFont(QFont("Timers", 20, QFont::Bold));
    nameEdit->setPlaceholderText(tr("输入账号"));

    //密码
    QLabel *passwordLable = new QLabel();
    passwordLable->setText(tr("密码"));
    passwordLable->setFont(ft);


    QLineEdit *passwordEdit = new QLineEdit();
    passwordEdit->setMaxLength(20);
    passwordEdit->setFont(QFont("Timers", 20, QFont::Bold));
    passwordEdit->setPlaceholderText(tr("输入密码"));

    //登录按钮
    QPushButton *loginButton = new QPushButton();
    //loginButton->setParent(this);
    loginButton->setText("登录");
    loginButton->setFont(ft);
    loginButton->setStyleSheet(
                "QPushButton{background-color:rgba(255,178,0,100%); font-size: 40px; color: white; border-radius: 10px; border: 2px groove gray; border-style: outset; padding: 15px 32px}"
                "QPushButton:hover{background-color:white; color: black;}"
                "QPushButton:pressed{background-color:rgb(85, 170, 255); border-style: inset; }"
                );

    //登录布局
    QGridLayout *loginLayout = new QGridLayout();
    loginLayout->addWidget(nameLabel, 0, 0);
    loginLayout->addWidget(nameEdit, 0, 1);

    loginLayout->addWidget(passwordLable, 1, 0);
    loginLayout->addWidget(passwordEdit, 1, 1);

    loginLayout->addWidget(loginButton, 2, 0, 1, 0);


//    //右上角关闭按钮最小化布局
//    QHBoxLayout *closeLayout = new QHBoxLayout();
//    closeLayout->addWidget(minLogin);
//    closeLayout->addWidget(closeLogin);
//    closeLayout->addStrut(100);



    //主布局
    QGridLayout *mainLayout = new QGridLayout(this);
    mainLayout->setMargin(200);
    //mainLayout->addLayout(closeLayout, 0, 100);
    mainLayout->addLayout(loginLayout, 3, 3);



    //配置保存 cookie保存
    //this->setToken("3333343");
    //this->getToken();

    //json操作
    //this->jsonPost();
    QPushButton *closeButton = new QPushButton(this);
    closeButton->setText("x");
    closeButton->setGeometry(720, 0, 80, 30);
    connect(closeButton, &QPushButton::clicked, this, &LoginWidget::closeWin);

    QPushButton *minButton = new QPushButton(this);
    minButton->setText("-");
    minButton->setGeometry(640, 0, 80, 30);
    connect(minButton, &QPushButton::clicked, this, &LoginWidget::hideWin);


    connect(loginButton, &QPushButton::clicked, this, [this, nameEdit, passwordEdit](){
        this->name = nameEdit->text();
        this->password = passwordEdit->text();
    });

    connect(loginButton, &QPushButton::clicked, this, &LoginWidget::sendSlot);
}

void LoginWidget::sendSlot()
{
    qDebug() << name << endl;
    qDebug() << password << endl;
    if (true) {
        emit loginSign();
        emit loginSign("token");
    }
    else
    {
        QMessageBox::warning(this, "错误信息", "<span style='color:red'>账号密码登录错误!</span>", "返回");
    }
}

void LoginWidget::closeWin()
{
    qDebug() << "lable:closesign";
    emit closeWinSign();
}

void LoginWidget::hideWin()
{
    showMinimized();
    //this->hide();
}

//void LoginWidget::paintEvent(QPaintEvent *)
//{
//    //QPainter p(this);
//    QPainter p;
//    p.begin(this);
//    p.drawPixmap(0, 0, width(), height(), QPixmap("../image/GameTest/time.png"));
//    p.end();
//}

bool LoginWidget::setToken(QString cookie)
{
    QSettings* settingini = new QSettings("setting.ini", QSettings::IniFormat);
    if (cookie == "")
    {
        return false;
    }
    else
    {
       settingini->setValue("login/cookie", cookie);
       delete settingini;
       return true;
    }
}

QString LoginWidget::getToken()
{
    QSettings* settingini = new QSettings("setting.ini", QSettings::IniFormat);
    QString token = settingini->value("login/token").toString();
    tokenValue = token;
    //qDebug() << cookie;
    return token;
}

void LoginWidget::jsonPost()
{
    QJsonObject jobj;
    jobj.insert("code", QJsonValue::fromVariant(2));
    jobj.insert("filed1", QJsonValue::fromVariant("monmkey"));
    jobj.insert("filed2", QJsonValue::fromVariant("human"));

    QJsonDocument doc(jobj);
//    qDebug() << doc.toJson();

    HttpCurl httpHand;
    QString url = "http://127.0.0.1:8080/product/list?token=111";
    //QString str = QString(httpHand.post(url, doc.toJson()));
    QByteArray reponseBytes = httpHand.get(url);
    qDebug() <<"获取的get的byteArr数据:" << reponseBytes << endl;

    //根据返回字符串做调整
//    QString str = QString(reponseBytes);
//    qDebug()<< "str:" << str;//此处打印应该有/n /r之类的字符串
    //如果返回的是json
//    QByteArray jsonBytes = str.toLocal8Bit();
    auto jsonDoc = QJsonDocument::fromJson(reponseBytes);
    QJsonObject json_obj = jsonDoc.object();
    QVariantMap json_map = json_obj.toVariantMap();
    qDebug() << "解析出来的code:" << json_map["code"].toInt();
    qDebug() << "解析出来的time:" << json_map["time"].toString();
    qDebug() << "解析出来的des:" << json_map["des"].toString();
    //qDebug() << "解析出来的data:" << json_map["data"];
    //解析第二层嵌套
    QVariant jsonMap2 = json_map["data"];
    QVariantMap json_map2 = jsonMap2.toMap();
    qDebug() << "解析出来的data内数据:" << json_map2["total"].toInt();

    //第三层嵌套
    QVariant jsonMap3 = json_map2["lists"];
    qDebug() << "解析出来的lists:" << jsonMap3;

    QList<QVariant> lists = jsonMap3.toList();
    //循环获取list
    foreach(QVariant list, lists)
    {

        QVariantMap arr = list.toMap();
        qDebug() << arr["id"].toInt() << endl;
    }
}

void LoginWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->buttons() == Qt::LeftButton)
    {
        flag = true;
        mousePoint = event->globalPos() - this->pos();
        event->accept();
    }
}
void LoginWidget::mouseMoveEvent(QMouseEvent *event)
{
if (flag && (event->buttons() && Qt::LeftButton) && event->pos().y() < 100 && event->pos().y() > 0) //后面这两个条件即是自己想要设定的鼠标接收范围
    {
        move(event->globalPos() - mousePoint);
        event->accept();
    }
}
void LoginWidget::mouseReleaseEvent(QMouseEvent *)
{
    flag = false;
}
